package com.iti.jets.gpts.home;

// default package
// Generated Mar 22, 2014 9:41:00 PM by Hibernate Tools 3.4.0.CR1
import com.iti.jets.gpts.dao.AbstractDAO;
import com.iti.jets.gpts.pojo.Feedback;
import com.iti.jets.gpts.pojo.TranService;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

/**
 * Home object for domain model class Feedback.
 *
 * @see .Feedback
 * @author Hibernate Tools
 */
public class FeedbackHome extends AbstractDAO<Feedback> {

    public FeedbackHome(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    public int calculateAverageFeedBack(int id) {
        int rate = 0;
        Criteria criteria = session.createCriteria(Feedback.class, "f")
                .add(Restrictions.eq("f.tranService.id", id));
        List<Feedback> result = criteria.list();
        int length = result.size();
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                rate += result.get(i).getRating();
            }
            return rate / length;
        } else {
            return 0;
        }
    }

    public int getAllFeedbacks() {

        Criteria criteria = session.createCriteria(Feedback.class);
        List<Feedback> result = criteria.list();
        return result.size();

    }

    public String getNumberOfFeedbacks(int id) {
        Criteria criteria = session.createCriteria(Feedback.class, "f")
                .add(Restrictions.eq("f.tranService.id", id));
        return Integer.toString(criteria.list().size());
    }

    public Feedback getObject(int id) {
        Criteria criteria = session.createCriteria(Feedback.class, "f")
                .add(Restrictions.eq("f.user.id", id));
        Feedback feedback = (Feedback) criteria.list().get(0);
        return feedback;

    }

}
